var AdminAssignRoleView = {
	getView : function(id, name, result) {
		var recordIds = new Array();
		var recordNames = new Array();
		var panel = this.initPanel(result, recordIds, recordNames);
		var window = new Ext.Window({
			title : "为人员[" + name + "]分配角色",
			width : 800,
			height : 550,
			layout : "fit",
			border : false,
			closeAction : "hide",
			items : [panel],
			resizable : true,
			modal : true,
			buttonAlign : "center",
			buttons : [{
				text : "保存",
				scope : "true",
				handler : function() {
					Ext.Msg.confirm('已选择角色', recordNames.toString(), function(
							btn, text) {
						if (btn == 'yes') {
							Ext.Ajax.request({
								url : __ctxPath + "/systemRole/insertAdminRole",
								method : "POST",
								params : {
									adminId : id,
									roles : recordIds.toString()
								},
								success : function(response, options) {
									var jsonData = Ext.util.JSON
											.decode(response.responseText);
									if (jsonData.success) {
										Ext.MessageBox.show({
													title : "信息提示",
													msg : "你已经成功为人员[<b>" + name
															+ "</b>]进行了角色分配",
													buttons : Ext.MessageBox.OK,
													icon : Ext.MessageBox.INFO
												});
										window.close();
									} else {
										Ext.MessageBox.show({
													title : "错误提示",
													msg : jsonData.data,
													buttons : Ext.MessageBox.OK,
													icon : Ext.MessageBox.ERROR
												});
									}
								},
								failure : function(response, options) {
									Ext.MessageBox.show({
												title : "错误提示",
												msg : "分配角色出错!",
												buttons : Ext.MessageBox.OK,
												icon : Ext.MessageBox.ERROR
											});
								},
								scope : this
							});
						}
					});
				}
			}, {
				text : "关闭",
				handler : function() {
					window.close();
				}
			}]
		});
		return window;
	},
	initPanel : function(result, recordIds, recordNames) {
		var sm = new Ext.grid.CheckboxSelectionModel({
					renderer : this.checkboxRenderer.createDelegate(this),
					listeners : {
						'rowselect' : function(sm, rowIndex, record) {
							if (recordIds.length > 0) {
								for (var i = 0; i < recordIds.length; i++) {
									if (record.data["id"] == recordIds[i]) {
										return;
									}
								}
							}
							recordIds.unshift(record.data["id"]);
							recordNames.unshift(record.data["name"]);
						},
						'rowdeselect' : function(sm, rowIndex, record) {
							for (var i = 0; i < recordIds.length; i++) {
								if (record.data["id"] == recordIds[i]) {
									recordIds.splice(i, 1);
									recordNames.splice(i, 1);
									return;
								}
							}
						}
					}
				});
		var column = new Ext.grid.ColumnModel({
					columns : [sm, new Ext.grid.RowNumberer(), {
								header : "角色名称",
								dataIndex : "name",
								width : 60
							}, {
								header : "角色描述",
								width : 100,
								dataIndex : "description"
							}],
					defaults : {
						sortable : true,
						menuDisabled : true,
						width : 120
					},
					listeners : {
						hiddenchange : function(n, o, p) {
							saveConfig(o, p);
						}
					}
				});
		var store = new Ext.data.Store({
					proxy : new Ext.data.HttpProxy({
								url : __ctxPath
										+ "/systemRole/selectRoleByDeptId"
							}),
					reader : new Ext.data.JsonReader({
								root : "list",
								totalProperty : "totalCount",
								fields : ["id", "name", "description",
										"isUseable", "updatedAt", "deptId",
										"createdAt", "createdMan",
										"updatedMan", "deptName"]
							}),
					remoteSort : true
				});
		var grid = new Ext.grid.EditorGridPanel({
					title : "角色列表",
					autoScroll : true,
					id : "adminAssignRoleGrid",
					region : "center",
					height : 360,
					autoWidth : true,
					store : store,
					shim : true,
					trackMouseOver : true,
					disableSelection : false,
					loadMask : true,
					cm : column,
					sm : sm,
					viewConfig : {
						forceFit : true,
						enableRowBody : false,
						showPreview : false
					},
					bbar : new PG.PagingBar({
								store : store,
								pageSize : 25
							})
				});
		store.on('load', function(st, records, options) {
					if (recordIds.length == 0) {
						for (var j = 0; j < result.length; j++) {
							recordIds.unshift(result[j].id);
							recordNames.unshift(result[j].name);
						}
					}
					if (recordIds.length > 0) {
						st.each(function(rec) {
									Ext.each(recordIds, function(item, index,
													allItems) {
												if (rec.data["id"] == item) {
													sm.selectRecords([rec],
															true);
													return false;
												}
											}, this);
								}, this);
					}
				}, this, {
					delay : 1000
				});
		store.load({
					params : {
						start : 0,
						limit : 25
					}
				});
		var searchForm = new Ext.FormPanel({
					id : "adminAssignRoleForm",
					height : 42,
					region : "north",
					layout : "hbox",
					bodyStyle : "padding:8px 2px 2px 2px",
					layoutConfigs : {
						align : "middle"
					},
					defaultType : "label",
					defaults : {
						margins : "0 4 0 4"
					},
					items : [{
								text : "角色名称"
							}, {
								xtype : "textfield",
								name : "name",
								width : 260
							}, {
								xtype : "button",
								text : "查询",
								width : 80,
								handler : function() {
									Ext.getCmp("adminAssignRole_deptId")
											.setValue('');
									if (searchForm.getForm().isValid()) {
										Ext.getCmp("adminAssignRoleGrid")
												.getStore().load();
									}
								}
							}, {
								name : "deptId",
								id : "adminAssignRole_deptId",
								xtype : "hidden"
							}]
				});
		var tree = new Ext.tree.TreePanel({
					id : "treePanels",
					autoScroll : true,
					title : "按部门分类 ",
					loader : new Ext.tree.TreeLoader({
								url : __ctxPath
										+ "/systemDept/selectDeptTreeById?deptRoot=1"
							}),
					root : new Ext.tree.AsyncTreeNode({
								expanded : true
							}),
					rootVisible : false,
					listeners : {
						"click" : this.clickNode
					}
				});
		tree.expandAll();
		grid.store.on('beforeload', function() {
					Ext
							.apply(this.baseParams, searchForm.getForm()
											.getValues());
				});
		var contactPanel = new Ext.Panel({
					id : "contactPanel",
					width : 575,
					height : 410,
					layout : "border",
					border : false,
					items : [searchForm, {
								region : "west",
								split : true,
								header : false,
								collapsible : true,
								width : 160,
								layout : "accordion",
								items : [tree]
							}, grid]
				});
		return contactPanel;
	},
	checkboxRenderer : function(value, cellmeta, record, rowIndex, columnIndex,
			store) {
		var row = Ext.getCmp("adminAssignRoleGrid").getStore()
				.getById(record.id);
		var id = row.get(this.id);
		var isSelect = false;
		if (!isSelect) {
			return '<div  class="x-grid3-row-checker">&nbsp;</div>';
		} else {
			return '';
		}
	},
	clickNode : function(b) {
		if (b != null) {
			Ext.getCmp("adminAssignRole_deptId").setValue(b.id);
			if (Ext.getCmp("adminAssignRoleForm").getForm().isValid()) {
				Ext.getCmp("adminAssignRoleGrid").getStore().load();
			}
		}
	}
};